
var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
var common = require('../../../utils/common.js')
var sysJson = require('../../../utils/NetWork/sysJson.js')
Page({


  data: {
    title: "",
    content: "",
    orgid: "",
    userid: "",
    perImgSrc: "../../../images/tup.png"
  },

  onLoad: function (options) {
    var orgid = common.getStorageSync("orgid");
    var userid = common.getStorageSync("userid");
    var g_title = common.getStorageSync("name");
    this.setData({
      orgid: orgid,
      userid: userid,
      g_title: g_title
    });
  },  title: function (e) {
    this.setData({
      title: e.detail.value
    })
  }, content: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  goBack: function () {
    if (this.data.content.replace(/(^\s*)|(\s*$)/g, "") == "" || this.data.title.replace(/(^\s*)|(\s*$)/g, "") == "") {
      common.showToast("内容不能为空", "none");
    } else if (this.data.title.length < 4 || this.data.title.length > 40) {
      common.showToast("标题必须4-40字", "none");
    } else if (this.data.content.length < 15 || this.data.content.length > 500) {
      common.showToast("内容必须15-500字", "none");
    } 
     else {
      let InfoJson = '{"orgid":"' + this.data.orgid + '","userid":"' + this.data.userid + '","classid":"e024c969-908b-43f0-aeac-8178d9232648","content":"' + this.data.content + '","id":"","title":"' + this.data.title + '","titlepics":"' + this.data.imgSrcid+'","username":"' + this.data.g_title + '","onclick":"0"}';
      let params = { "InfoJson": InfoJson, "orgid": this.data.orgid, "userid": this.data.userid, };
      //第三步 调用接口
      businessLogic.AddAMessage(params, (res) => {
        if (res.flag == 1) {
          common.showToast("成功", "succeed");
          wx.navigateTo({
            url: '../information/information',
          })


        } else {
          common.showToast("失败", "fail");
        }

      });
    }
  },

  chooseImg: function () {
    var that = this;
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.info(res.tempFilePaths.length);
        that.uploadFile(tempFilePaths, 0);
      }
    })
  },

  uploadFile: function (file, i) {
    console.log("file路径为" + file);
    var that = this;
    wx.uploadFile({
      url: 'https://tizi.xingzi.org.cn/tizi/pub/admin/system/enclosure/uploadFileOne.do', 
      filePath: file[i],
      name: 'file',
      header: { "Content-Type": "multipart/form-data" },

      formData: {
        //和服务器约定的token, 一般也可以放在header中
        'session_token': wx.getStorageSync('session_token')
      },
      success: function (res) {
        var obj = new Object();
        obj.ind = i + 1;
        var data = res.data;
        console.info(res)

        data = JSON.parse(data);

        obj.src = data;
        console.info("---------------------------------");
        console.info(obj);
        console.log(obj.src.result.id);
        // that.data.perImgSrc.push(obj);
        
        that.setData({
          perImgSrc: "https://tizi.xingzi.org.cn/tizi/pub/admin/" +"system/enclosure/findimgfile.do?fileid=" + obj.src.result.id,
          imgSrcid: obj.src.result.id
        })
      }
    })
  }, 

  onReady: function () {

  },


  onShow: function () {

  },


  onHide: function () {

  },


  onUnload: function () {

  },


  onPullDownRefresh: function () {

  },


  onReachBottom: function () {

  },


  onShareAppMessage: function () {

  }
}) 







